﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FirstFloor.ModernUI.Presentation;
using DatabaseCore;
using System.Collections.ObjectModel;
using FirstFloor.ModernUI.Windows.Controls;
using System.Windows.Controls;
using System.Windows;

namespace Export.Ui.ViewModel.Admin
{
    public class UserMaintananceViewModel : ViewModelBase
    {
        private MainWindowViewModel _mainWindowViewModel;
        private UserContext _currentUser;
        private ObservableCollection<tbl_User> _users;
        private IUserDao _userDao = new UserDao();
        private RelayCommand SaveCommand = null;

        public UserMaintananceViewModel(MainWindowViewModel mainWindowViewModel)
        {
            _mainWindowViewModel = mainWindowViewModel;
            initialUser();
            InitialCommand();
        }

        public ObservableCollection<tbl_User> Users
        {
            get
            {
                _users = _users ?? new ObservableCollection<tbl_User>();
                return _users;
            }
        }

        public RelayCommand MouseDoubleClickCommand { get; set; }
        public RelayCommand NewCommand { get; set; }
        public RelayCommand EditCommand { get; set; }

        public tbl_User SelectedUser { get; set; }

        public int SelectedUserIndex { get; set; }

        private void InitialCommand()
        {
            MouseDoubleClickCommand = new RelayCommand(ShowUserDetails);
            NewCommand = new RelayCommand(ShowUserDetailsNew);
            EditCommand = new RelayCommand(ShowUserDetails);
        }

        public ModernDialog dialog { get; set; }

        private void ShowUserDetails(object parameter)
        {
            tbl_User selected = (tbl_User)parameter ?? SelectedUser;
            if (selected == null)
            {
                MessageBox.Show("Please select user to edit");
                return;
            }

            dialog = new ModernDialog
            {
                Title = "Edit User " + selected.FullName,
                Content = new Pages.Admin.UserDetails(selected,this),
                SizeToContent = System.Windows.SizeToContent.WidthAndHeight,
                Width = 500,
                Height = 500,
                
            };
            Button hidden = new ModernButton();
            hidden.Visibility = System.Windows.Visibility.Hidden;
            dialog.Buttons = new Button[] { hidden };
            var test =  dialog.ShowDialog();
        }

        private void ShowUserDetailsNew(object parameter)
        {
            dialog = new ModernDialog
            {
                Title = "New User",
                Content = new Pages.Admin.UserDetails(this) ,
                SizeToContent = System.Windows.SizeToContent.WidthAndHeight,
                Width = 500,
                Height = 500,

            };
            Button hidden = new ModernButton();
            hidden.Visibility = System.Windows.Visibility.Hidden;
            dialog.Buttons = new Button[] { hidden };
            var test = dialog.ShowDialog();
        }

        private void initialUser()
        {
            foreach (tbl_User u in _userDao.GetAllUser())
            {
                Users.Add(u);
            }
        }
    }
}
